/*
 * AddTrait.cpp
 *
 *  Created on: Jan 12, 2009
 *      Author: sez
 */

#include "ui_addtrait.h"
#include "AddTrait.h"

#include <DndData/Trait.h>

AddTrait::AddTrait(QWidget *parent)
    : AddUpdateDialog(XmlPath(XmlPath::CLASS_BASE), parent)
    , mAddTrait(new Ui::AddTraitDialog())
    , mData(0)
{
    mAddTrait->setupUi(this);
    XmlPath xp(xmlPath());
    xp.setSubType(XmlPath::TRAIT_TYPE);
    setXmlPath(xp);
}

AddTrait::~AddTrait()
{
    delete mAddTrait;
    delete mData;
}

/*!
  \brief Return the instance of the AddTrait dialog.
  This method is first called during the construction of the QMainWindow,
  and at this time the instance is initialized with the main window as its
  \a parent.  With this invariant, the method is thread-safe, otherwise it
  is not.  On subsequent calls the already initialized instance is returned
  and any argument is ignored.
 */
AddTrait *AddTrait::dialog(QWidget *parent)
{
    static AddTrait *dlg = 0;
    if (dlg == 0)
        dlg = new AddTrait(parent);
    return dlg;
}

TitledData *AddTrait::data()
{
    if (!mData)
        mData = new Trait(xmlPath());
    return mData;
}

void AddTrait::setupData()
{
    mAddTrait->titleLabel->setText(xmlPath().objectName());
    QString base = XmlPath::BASE_TYPE_NAMES[xmlPath().type()];
    QString summary = xmlPath().typeName() + " " + base + " Trait";
    mAddTrait->summaryLabel->setText(summary);
}

void AddTrait::saveData()
{
    mData->summary = mAddTrait->summaryLineEdit->text();
    mData->details = mAddTrait->detailsPlainTextEdit->document()->toPlainText();
}

void AddTrait::loadData()
{
    //mAddTrait->
    mAddTrait->detailsPlainTextEdit->document()->setPlainText(mData->details);
    mAddTrait->summaryLineEdit->setText(mData->summary);
}

void AddTrait::clearData()
{
    delete mData;
    mData = 0;
    mAddTrait->detailsPlainTextEdit->document()->setPlainText("");
    mAddTrait->summaryLineEdit->setText("");
}
